package com.cfxy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.cfxy.model.User;
import com.cfxy.util.JdbcUtil;

public class UserDao {
	Connection conn = JdbcUtil.getConnection();
	
	
	public void insert(String username,String password) {
		PreparedStatement ps = null;
		try {
			String sql = "insert into user(username,password) value(?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1,username);
			ps.setString(2,password);
			int n = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	public void insert(User user) {
		PreparedStatement ps = null;
		try {
			String sql = "insert into user(username,password) value(?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1,user.getUsername());
			ps.setString(2,user.getPassword());
			int n = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	
	public void delet(User user){
		PreparedStatement ps = null;
		String sql = "delete from user where username = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delet(String name){
		PreparedStatement ps = null;
		String sql = "delete from user where username = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,name);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delet(int id){
		PreparedStatement ps = null;
		String sql = "delete from user where id = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1,id);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void upDate(User user){
		PreparedStatement ps = null;
		String sql = "update user set password = ?,username = ? where id = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, user.getPassword());
			ps.setString(2, user.getUsername());
			ps.setInt(3, user.getId());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void upDate(String name,String psw){
		PreparedStatement ps = null;
		String sql = "update user set password = ? where username = ?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,name);
			ps.setString(2,psw);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public List getAll(){
		PreparedStatement ps = null;
		List<User> list = new ArrayList<User>();
		String sql = "select * from user";
		try {
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				list.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	public User find(String name,String psw){
		User user = new User();
		PreparedStatement ps = null;
		String sql = "select * from user where username=?and password=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1,name);
			ps.setString(2,psw);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
			}else {
				user =null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
	public User find(int id){
		User user = new User();
		PreparedStatement ps = null;
		String sql = "select * from user where id=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1,id);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				user.setId(rs.getInt("id"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
			}else {
				user =null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return user;
	}
}
